package com.example.moviecms2.controller;

import com.example.moviecms2.domain.Movie;
import com.example.moviecms2.repository.MovieRepository;
import com.example.moviecms2.utils.ResultVOUtils;
import com.example.moviecms2.vo.MovieVO;
import com.example.moviecms2.vo.RestfulVO;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 杨友奇
 * @Time 2018/4/26
 */
@RestController
@RequestMapping("/api/movie")
public class MovieRestfulController {
    @Autowired
    private MovieRepository movieRepository;


    //查询电影列表
    @RequestMapping(value = "lists", method = RequestMethod.GET)
    public RestfulVO<List<MovieVO>> list() {
        List<Movie> list = movieRepository.findAll();

        List<MovieVO> voList = new ArrayList<>();

        for (Movie movie : list) {
            MovieVO vo = new MovieVO();
            vo.setId(movie.getId());
            vo.setName(movie.getName());
            vo.setPhoto(movie.getPhoto());
            voList.add(vo);
        }
        return ResultVOUtils.success(voList);
    }


    //创建一个电影
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public RestfulVO<Movie> add(@RequestParam String name, @RequestParam String photo, String createDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = format.parse(createDate);
        } catch (Exception e) {

        }

        Movie movie = new Movie();
        movie.setName(name);
        movie.setPhoto(photo);
        movie.setCreateDate(date);
        Movie movie1 = movieRepository.save(movie);
        if (movie1 != null){
           return ResultVOUtils.success(movie1);
        }else {
            return ResultVOUtils.fail();
        }

    }

    //更据ID查询电影
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public RestfulVO<MovieVO> detail(@PathVariable Long id) {
        Movie movie = movieRepository.findOne(id);
        MovieVO movieVO = new MovieVO(movie);

        /*RestfulVO restfulVO = new RestfulVO();
        restfulVO.setCode(200);
        restfulVO.setMsg("成功");
        restfulVO.setData(movieVO);
        return restfulVO;*/
        return ResultVOUtils.success(movieVO);
    }

    //根据ID修改电影
    //required : 表示可以不传参数
    //修改：对传入的参数只对电影名称进行修改，不对其他信息进行修改
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String doEdit(@PathVariable Long id,
                         @RequestParam String name) {

        Movie movie = movieRepository.findOne(id);
        movie.setName(name);

        System.out.println(movie.getName());
        System.out.println(movie.getPhoto());
        System.out.println(movie.getCreateDate());
        movie.setPhoto(movie.getPhoto());
        movie.setCreateDate(movie.getCreateDate());
        Movie result = movieRepository.save(movie);
        return result != null ? "更新成功" : "更新失败";
    }

    //删除ID
    //value:绑定URL中ID的变量
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public RestfulVO<String> delete(@PathVariable(value = "id") Long id) {
        movieRepository.delete(id);
        return ResultVOUtils.success(null);
    }
}
